package kit.edu.lego.gruppe3.levels.sumpf;

import kit.edu.lego.gruppe3.ComponentsRepository;
import kit.edu.lego.gruppe3.LegoMain;
import kit.edu.lego.gruppe3.levels.LevelSelector;
import kit.edu.lego.gruppe3.levels.LevelSelector.Level;
import lejos.nxt.LightSensor;
import lejos.nxt.NXTRegulatedMotor;
import lejos.robotics.navigation.DifferentialPilot;
import lejos.robotics.subsumption.Behavior;

public class DarkGround implements Behavior{

	private LightSensor lightS = ComponentsRepository.getInstance().getLightSensor();
	private DifferentialPilot robot = ComponentsRepository.getInstance().getRobotPilot();
	private NXTRegulatedMotor motor = ComponentsRepository.getInstance().getMiddleMotor();
	

	
	@Override
	public void action() 
	{
		if (!SumpfLineCrossed.getInstance().lastCrossingWasDark())
		{
			SumpfLineCrossed.getInstance().crossDarkArea();
		}
		
		robot.setTravelSpeed(6);
		robot.forward();

		if (SumpfLineCrossed.getInstance().isSumpfFinished())
		{
			System.out.println("Next level");
			motor.rotate(ComponentsRepository.INIT_SENSORS_ANGLE);
			
			while(ComponentsRepository.getInstance().getLightSensor().getLightValue() 
					> ComponentsRepository.LIGHT_TRASHOLD)
			{
				ComponentsRepository.getInstance().getRobotPilot().forward();
			}
			
			SumpfSolver.setLevelCleared(true);
			LevelSelector.getInstance().setCurrentLevel(Level.labyrinth);
			LegoMain.startLabyrinth();

		}
		

	}

	@Override
	public void suppress()
	{

	}

	@Override
	public boolean takeControl() 
	{
		return LevelSelector.getInstance().isSumpf() &&
			lightS.getLightValue() < ComponentsRepository.LIGHT_TRASHOLD;
	}
}
